﻿using System.Collections;
using UnityEngine;
using YooAsset;

public class Boot : MonoBehaviour
{

    [SerializeField]
    private BootSettings settings = new();


    void Awake()
    {

        DontDestroyOnLoad(this.gameObject);
    }
    public void SetIP(string ip)
    {
        settings.HostServerIP = ip;
    }

    public void OnStart()
    {
        StartCoroutine(Run());
    }

    IEnumerator Run()
    {
        SetMessageHelper();

        // 游戏管理器
        BootManager.Instance.Behaviour = this;
        // 初始化资源系统
        YooAssets.Initialize();

        // 加载更新页面
        var go = Resources.Load<GameObject>("PatchWindow");
        Instantiate(go);

        //第一次打开逻辑
        VerifyBootMode();

        // 开始补丁更新流程
        PatchOperation operation = new PatchOperation(settings);
        YooAssets.StartOperation(operation);
        yield return operation;

        // 设置默认的资源包
        var gamePackage = YooAssets.GetPackage("DefaultPackage");
        YooAssets.SetDefaultPackage(gamePackage);
        yield return 0;
        //   Debug.Log("LoadDll");
        gameObject.AddComponent<LoadDll>();

    }


    /// <summary>
    /// Set Message Helper
    /// </summary>
    private void SetMessageHelper()
    {
        MessagingHelper.playMode = settings.PlayMode;
        MessagingHelper.applicableGameVersion = settings.ApplicableGameVersion;
        MessagingHelper.hostIpAddress = settings.HostServerIP;
    }

    /// <summary>
    /// 验证启动模式
    /// </summary>
    private void VerifyBootMode()
    {
        //第一次打开默认进入单机模式
        if (PlayerPrefs.GetString(PlayerPrefsKey.FirstVersion) == string.Empty)
        {
            Debug.Log("第一次打开");
            settings.PlayMode = EPlayMode.OfflinePlayMode;
        }
        else if (PlayerPrefs.GetInt(PlayerPrefsKey.ManualUpdate, 0) == 0
                 && PlayerPrefs.GetString(PlayerPrefsKey.LocalVersion) == PlayerPrefs.GetString(PlayerPrefsKey.FirstVersion))
        {
            //没点更新且本地版本号和首包版本号相同
            //进入单机模式
            settings.PlayMode = EPlayMode.OfflinePlayMode;
        }
        else
        {
            settings.PlayMode = EPlayMode.HostPlayMode;
        }

        Debug.Log($"资源系统运行模式：{settings.PlayMode}");
    }
}

[System.Serializable]
public class BootSettings
{

    [InspectorName("构造管线"), SerializeField]
    private EDefaultBuildPipeline buildPipeline = EDefaultBuildPipeline.BuiltinBuildPipeline;

    [InspectorName("资源系统运行模式"), SerializeField]
    private EPlayMode playMode = EPlayMode.EditorSimulateMode;

    [InspectorName("默认资源包"), SerializeField]
    private string defaultPackage = "DefaultPackage";

    [InspectorName("服务器地址"), SerializeField]
    private string hostServerIP;

    [InspectorName("获取当前资源适用的游戏版本号"), SerializeField]
    private string applicableGameVersion;

    [InspectorName("设置同时最大下载数目"), SerializeField]
    private int downloadingMaxNum = 10;

    [InspectorName("设置资源包下载失败后尝试次数"), SerializeField]
    private int failedTryAgain = 3;


    public int DownloadingMaxNum { get => downloadingMaxNum; }
    public int FailedTryAgain { get => failedTryAgain; }
    public EPlayMode PlayMode
    {
        get
        {
#if UNITY_EDITOR
            return (EPlayMode)UnityEditor.EditorPrefs.GetInt("EditorPlayMode");
#else
            return playMode;
#endif
        }
        set
        {
            playMode = value;
        }
    }
    public string HostServerIP { get => hostServerIP; set { hostServerIP = value; } }
    public string ApplicableGameVersion { get => applicableGameVersion; }
    public string DefaultPackage { get => defaultPackage; }

    public EDefaultBuildPipeline BuildPipeline { get => buildPipeline; }
}